<?php
/**
 * @title 100129. 回文串重新排列查询
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $s
     * @param Integer[][] $queries
     * @return Boolean[]
     */
    function canMakePalindromeQueries($s, $queries) {
        /**
         * @since 2023-12-31 字符串长度
         */
        $sLength = strlen($s);
        $halfLength = $sLength>>1;

        /**
         * @since 2023-12-31 字符数量
         */
        $charArray = [
            'a' => 0,
            'b' => 0,
            'c' => 0,
            'd' => 0,
            'e' => 0,
            'f' => 0,
            'g' => 0,
            'h' => 0,
            'i' => 0,
            'j' => 0,
            'k' => 0,
            'l' => 0,
            'm' => 0,
            'n' => 0,
            'o' => 0,
            'p' => 0,
            'q' => 0,
            'r' => 0,
            's' => 0,
            't' => 0,
            'u' => 0,
            'v' => 0,
            'w' => 0,
            'x' => 0,
            'y' => 0,
            'z' => 0,
        ];

        /**
         * @since 2023-12-31 初始化
         */
        $leftHashArray = $rightHashArray = $charArray;

        /**
         * @since 2023-12-31 左边、右边字符对应数量
         */
        for($left=0, $right=$sLength-1; $left<$halfLength; $left++, $right--){
            /**
             * @since 2023-12-31 对应字符
             */
            $leftChar = $s[$left];
            $rightChar = $s[$right];

            /**
             * @since 2023-12-31 统计数量
             */
            $leftHashArray[$leftChar]++;
            $rightHashArray[$rightChar]++;
        }

        /**
         * @since 2023-12-31 数组数量
         */
        $queryCount = count($queries);

        /**
         * @since 2023-12-31 判断是否有解
         */
        if($leftHashArray != $rightHashArray){
            /**
             * @return Boolean[]
             */
            return array_fill(0, $queryCount, false);
        } else {}

        /**
         * @since 2023-12-31 字符串切成2个字符串
         */
        $leftString = substr($s, 0, $halfLength);
        $rightString = strrev(substr($s, 0-$halfLength));

        /**
         * @since 2023-12-31 判断每个问题的结果
         */
        $queryResultArray = [];
        foreach ($queries as list($a, $b, $c, $d)){
            /**
             * @since 2023-12-31 反转一下的位置
             */
            $d = $sLength-1 - $d;
            $c = max($sLength-1 - $c, $sLength-$halfLength);
            continue = false;

            /**
             * @since 2023-12-31 统计[a,b]和[c,d]区间之外的字符，是否相同
             */
            for($i=0; $i<min($a, $c); $i++){
                if($leftString[$i] != $rightString[$i]){
                    $queryResultArray[] = false;
                    $continue = true;
                    break;
                } else {}
            }
            if($continue){
                continue;
            } else {}
            for($i=max($b, $d)+1; $i<$sLength; $i++){
                if($leftString[$i] != $rightString[$i]){
                    $queryResultArray[] = false;
                    $continue = true;
                    break;
                } else {}
            }
            if($continue){
                continue;
            } else {}


            /**
             * @since 2023-12-31 无交叉
             *
             * a  b
             *       c  d
             */
            if($b < $c) {
                for($i=$b+1; $i<$c; $i++){
                    if($leftString[$i] != $rightString[$i]){
                        $queryResultArray[] = false;
                        $continue = true;
                        break;
                    } else {}
                }
                if($continue){
                    continue;
                } else {}
            } elseif($d < $a){
                /**
                 *        a  b
                 * c  d
                 */
                for($i=$d+1; $i<$a; $i++) {
                    if ($leftString[$i] != $rightString[$i]) {
                        $queryResultArray[] = false;
                        $continue = true;
                        break;
                    } else {
                    }
                }
                if($continue){
                    continue;
                } else {}
            } else {}

            /**
             * @since 2023-12-31 统计[a,b]和[c,d]区间内的字符数量是否相同
             */
            $leftHashArray = $rightHashArray = $charArray;
        }

        /**
         * @return Boolean[]
         */
        return $queryResultArray;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.31_4.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
